import axios from "axios";
import { Notify } from "quasar";

// 创建axios实例
const service = axios.create({
  baseURL: "http://localhost:3000",
  timeout: 15000
});

// request拦截器
service.interceptors.request.use(
  config => {
    config.headers["Content-Type"] = "application/json";
    return config;
  },
  error => {
    Promise.reject(error);
  }
);

// response拦截器
service.interceptors.response.use(
  response => {
    const res = response;
    if (res.status !== 200) {
      if (res.status === 401 || res.status === 402 || res.status === 407) {
        Notify.notify({
          color: "secondary",
          textColor: "#fff",
          icon: "check",
          position: "top",
          message: "登录已过期"
        });
      }
      return Promise.reject(res);
    } else {
      return response.data;
    }
  },
  error => {
    if (error && error.response) {
      alert("请求错误");
    }
    return Promise.reject(error);
  }
);

export default service;
